/**
 * Copyright 2018
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed yiliao in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.example.common;

import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 日期处理
 */
public class MyDateUtils {
    /**
     * 时间格式(yyyy-MM-dd)
     */
    public final static String DATE_month_PATTERN = "yyyy-MM";

    /**
     * 时间格式(yyyy-MM-dd)
     */
    public final static String DATE_PATTERN = "yyyy-MM-dd";
    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String DATE_minute_PATTERN = "yyyy-MM-dd HH:mm";

    public static SimpleDateFormat SDF_FULL = new SimpleDateFormat(DATE_TIME_PATTERN);
    public static SimpleDateFormat SDF_YMD = new SimpleDateFormat(DATE_PATTERN);


    /**
     * 获取指定日期对应 日
     *
     * @param date
     * @return
     */
    public static int getDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        return day;
    }

    /**
     * 获取指定日期对应 年
     *
     * @param date
     * @return
     */
    public static int getMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int month = cal.get(Calendar.MONTH) + 1;
        return month;
    }


    /**
     * 获取指定日期对应 年
     *
     * @param date
     * @return
     */
    public static int getYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int year = cal.get(Calendar.YEAR);
        return year;
    }


    /**
     * 秒转日期
     */
    public static Date secondToDate(long second) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(second * 1000);//转换为毫秒

        Date date = calendar.getTime();
        return date;

    }

    /**
     * 豪秒转日期
     */
    public static Date millSecondToDate(long millSecond) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millSecond);
        Date date = calendar.getTime();
        return date;

    }

    /**
     * 获取当前时间戳 精确到秒
     *
     * @return
     */
    public static String getNowTimeStamp() {
        long time = System.currentTimeMillis();
        return String.valueOf(time / 1000);
    }

    /**
     * 日期格式化 日期格式为：yyyy-MM
     *
     * @param date 日期
     * @return 返回yyyy-MM格式日期
     */
    public static String formatMM(Date date) {
        return format(date, DATE_month_PATTERN);
    }

    /**
     * 日期格式化 日期格式为：yyyy-MM-dd
     *
     * @param date 日期
     * @return 返回yyyy-MM-dd格式日期
     */
    public static String formatD(Date date) {
        return format(date, DATE_PATTERN);
    }

    /**
     * 日期格式化 日期格式为：yyyy-MM-dd HH:mm:ss
     *
     * @param date 日期
     */
    public static String formatS(Date date) {
        return format(date, DATE_TIME_PATTERN);
    }

    /**
     * 日期格式化 日期格式为：yyyy-MM-dd HH:mm
     *
     * @param date 日期
     * @return 返回yyyy-MM-dd格式日期
     */
    public static String formatM(Date date) {
        return format(date, DATE_minute_PATTERN);
    }

    /**
     * 日期格式化 日期格式为：yyyy-MM-dd
     *
     * @param date    日期
     * @param pattern 格式，如：DateUtils.DATE_TIME_PATTERN
     * @return 返回yyyy-MM-dd格式日期
     */
    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }

    /**
     * 字符串转换成日期
     *
     * @param strDate 日期字符串
     * @param pattern 日期的格式，如：DateUtils.DATE_TIME_PATTERN
     */
    public static Date stringToDate(String strDate, String pattern) {
        if (StringUtils.isBlank(strDate)) {
            return null;
        }

        DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
        return fmt.parseLocalDateTime(strDate).toDate();
    }

    /**
     * 根据时间转换为时间戳
     * @param date
     * @param timestampType 转换类型 0毫秒 1秒
     * @return
     */
    public static long getTimeStamp(Date date, int timestampType) {
        long times = date.getTime();
        if (timestampType == 1) {
            times = times / 1000L;
        }
        return times;
    }
    /**
     * 根据时间转换为时间戳
     * @param date1 开始时间
     * @param date2 结束时间
     * @return
     */
    public static Integer getDateBetween(Date date1, Date date2){
        //获取当前时间
        Calendar date = Calendar.getInstance();
        date.setTime(new Date());
        //获取开始时间
        Calendar begin = Calendar.getInstance();
        begin.setTime(date1);
        //获取结束时间
        Calendar end = Calendar.getInstance();
        end.setTime(date2);
        if (date.before(begin)){
            return 1;
        }
        if (date.after(end)){
            return 2;

        }
        if ((date.after(begin) && date.before(end)) ||
                (date.getTime() == begin.getTime() || date.getTime() == end.getTime())) {
            return 3;

        }
        return 0;
    }

    /**
     * 时间戳转时间
     * @param timestamp
     * @param timestampType 时间戳格式 0毫秒 1秒
     * @return
     */
    public static String getDateTime(long timestamp, int timestampType)
    {
        String res;
        if (timestampType == 1)
        {
            //如果时间戳格式是秒，需要江时间戳变为毫秒
            timestamp = timestamp * 1000L;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dateTime = new Date(timestamp);
        res = simpleDateFormat.format(dateTime);
        return  res;

    }

    /**
     * 根据周数，获取开始日期、结束日期
     *
     * @param week 周期  0本周，-1上周，-2上上周，1下周，2下下周
     * @return 返回date[0]开始日期、date[1]结束日期
     */
    public static Date[] getWeekStartAndEnd(int week) {
        DateTime dateTime = new DateTime();
        LocalDate date = new LocalDate(dateTime.plusWeeks(week));

        date = date.dayOfWeek().withMinimumValue();
        Date beginDate = date.toDate();
        Date endDate = date.plusDays(6).toDate();
        return new Date[]{beginDate, endDate};
    }

    /**
     * 对日期的【秒】进行加/减
     *
     * @param date    日期
     * @param seconds 秒数，负数为减
     * @return 加/减几秒后的日期
     */
    public static Date addDateSeconds(Date date, int seconds) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusSeconds(seconds).toDate();
    }

    /**
     * 对日期的【分钟】进行加/减
     *
     * @param date    日期
     * @param minutes 分钟数，负数为减
     * @return 加/减几分钟后的日期
     */
    public static Date addDateMinutes(Date date, int minutes) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusMinutes(minutes).toDate();
    }

    /**
     * 对日期的【小时】进行加/减
     *
     * @param date  日期
     * @param hours 小时数，负数为减
     * @return 加/减几小时后的日期
     */
    public static Date addDateHours(Date date, int hours) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusHours(hours).toDate();
    }

    /**
     * 对日期的【天】进行加/减
     *
     * @param date 日期
     * @param days 天数，负数为减
     * @return 加/减几天后的日期
     */
    public static Date addDateDays(Date date, int days) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusDays(days).toDate();
    }

    /**
     * 对日期的【周】进行加/减
     *
     * @param date  日期
     * @param weeks 周数，负数为减
     * @return 加/减几周后的日期
     */
    public static Date addDateWeeks(Date date, int weeks) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusWeeks(weeks).toDate();
    }

    /**
     * 对日期的【月】进行加/减
     *
     * @param date   日期
     * @param months 月数，负数为减
     * @return 加/减几月后的日期
     */
    public static Date addDateMonths(Date date, int months) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusMonths(months).toDate();
    }

    /**
     * 对日期的【年】进行加/减
     *
     * @param date  日期
     * @param years 年数，负数为减
     * @return 加/减几年后的日期
     */
    public static Date addDateYears(Date date, int years) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusYears(years).toDate();
    }

    /**
     * 获取前一天的日期
     *
     * @return 前一天的日期
     */
    public static String getNowBeforeDate() {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.DATE, -1);
        date = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = format.format(date);
        return dateString;
    }

    /**
     * 获取当前秒数
     *
     * @return
     */
    public static int getNowSecond() {
        Long second = System.currentTimeMillis() / 1000;
        return second.intValue();

    }

    public static boolean checkDay(String withdrawDay) {
        boolean allowWithdraw = false;
        if (StringUtils.isNotEmpty(withdrawDay)) {
            String[] withdrawDayArr = withdrawDay.split(",");
            int currentDay = Calendar.getInstance().get(Calendar.DATE);
            for (String day : withdrawDayArr) {
                if (StringUtils.isNotEmpty(day)) {
                    if (Integer.valueOf(day).intValue() == currentDay) {
                        allowWithdraw = true;
                    }
                }
            }
        }
        return allowWithdraw;
    }

    public static String getWeek(Date date) {
        String[] weeks = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index < 0) {
            week_index = 0;
        }
        return weeks[week_index];
    }

    /**
     * 拼接时间戳，0:上午,1:下午,2:晚上，0-13点为上午;13-19点为下午;19-24点为晚上 例 "2019-08-08" + 0 ->
     * 2019-08-08 00：00：00
     *
     * @return
     */
    public static Date[] stitchDate(String date, int time) {
        String ymd = formatDateYMD(parseStringToDate(date));
        if (StringUtils.isEmpty(ymd)) {
            ymd = formatDateYMD(new Date());
        }
        Date[] dates = new Date[2];
        if (time == 0) {
            dates[0] = parseStringToDate(ymd + " " + "00:00:00");
            dates[1] = parseStringToDate(ymd + " " + "13:00:00");
        } else if (time == 1) {
            dates[0] = parseStringToDate(ymd + " " + "13:00:00");
            dates[1] = parseStringToDate(ymd + " " + "19:00:00");
        } else if (time == 2) {
            dates[0] = parseStringToDate(ymd + " " + "19:00:00");
            dates[1] = parseStringToDate(ymd + " " + "23:59:59");
        }
        return dates;
    }

    /**
     * 根据开始和结束时间推算日期和所处时间段的int值； 0:上午,1:下午,2:晚上，0-13点为上午;13-19点为下午;19-24点为晚上
     *
     * @param begin 开始
     * @param end   结束
     * @return yyyy-MM-dd & int
     */
    public static Object[] calcDateAndRange(Date begin, Date end) {
        int m = 1;
        if (end.getHours() <= 13) {
            m = 0;
        } else if (begin.getHours() >= 19) {
            m = 2;
        }

        Object[] objects = new Object[2];

        objects[0] = formatDateYMD(begin);
        objects[1] = m;
        return objects;
    }

    /**
     * 获取时间是在这天的上午、下午、还是晚上 0:上午,1:下午,2:晚上
     */
    public static Integer jargeTimeRange(String dateString) {
        Date date = null;
        if (StringUtils.isEmpty(dateString)) {
            return null;
        }

        try {
            date = SDF_FULL.parse(dateString);
        } catch (ParseException e) {
            try {
                date = SDF_YMD.parse(dateString);
            } catch (ParseException e1) {
                e1.printStackTrace();
            }
        }
        return jargeTimeRange(date);
    }

    /**
     * 比较第一个日期是否大于第二个日期
     * @param firstDate 第一个日期
     * @param secondDate 第二个日期
     * @return true-大于;false-不大于
     */
    public boolean localDateIsBefore(LocalDate firstDate, LocalDate secondDate) {
        return firstDate.isBefore(secondDate);
    }

    /**
     * 获取时间是在这天的上午、下午、还是晚上 0:上午,1:下午,2:晚上
     */
    public static int jargeTimeRange(Date date) {
        /**
         * 预约时间,0:上午,1:下午,2:晚上
         *
         * 7，13，19
         */
        if (null == date) {
            return -1;
        }
        int resvTime;
        if (date.getHours() < 13) {
            resvTime = 0;
        } else if (date.getHours() >= 13 && date.getHours() < 19) {
            resvTime = 1;
        } else {
            resvTime = 2;
        }
        return resvTime;
    }

    /**
     * 获取X天前日期
     *
     * @param day 天
     */
    public static String getDate(int day) {
        StringBuffer s = new StringBuffer();
        Calendar c = Calendar.getInstance();
        int currentDay = c.get(Calendar.DATE);
        if (day < 0) {
            c.add(Calendar.YEAR, -1);
            c.set(Calendar.DATE, currentDay);
        } else if (day == 29) {
            c.add(Calendar.MONTH, -1);
            c.set(Calendar.DATE, currentDay);
        } else {
            c.add(Calendar.DATE, -day);
        }

        s.append(c.get(Calendar.YEAR) + "-");
        s.append((c.get(Calendar.MONTH) + 1) < 10 ? ("0" + (c.get(Calendar.MONTH) + 1)) : (c.get(Calendar.MONTH) + 1));
        s.append("-");
        s.append(c.get(Calendar.DATE) < 10 ? ("0" + c.get(Calendar.DATE)) : c.get(Calendar.DATE));
        return s.toString();
    }

    /**
     * @return 00:00：00
     */
    public static Date getMonthBeginTime() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        return c.getTime();
    }

    /**
     * @param date
     * @return 00:00：00
     */
    public static Date getBeginTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        return c.getTime();
    }

    /**
     * @param date
     * @return 23:59：59
     */
    public static Date getEndTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        return c.getTime();
    }

    /**
     * 获取当前时间的yyyy-MM-dd格式字符串
     *
     * @return 当前时间的yyyy-MM-dd格式字符串
     */
    public static String getNow() {
        return getNow("yyyy-MM-dd");
    }

    /**
     * 获取当前时间指定格式字符串
     *
     * @param pattern 日期时间格式(如:yyyy-MM-dd,MM-dd-yyyy等)
     * @return 当前时间指定格式字符串
     */
    public static String getNow(String pattern) {
        Calendar now = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(now.getTime());
    }

    /**
     * @param date1 需要比较的时间
     * @param date2 被比较的时间
     * @param type  需要返回的差值类型，0s, 1m, 2h, 3d
     * @return 时间差值
     */
    public static long compareDate(Date date1, Date date2, int type) {
        long time = date1.getTime() - date2.getTime();
        if (type > -1) {
            time = time / 1000;
        }
        if (type > 0) {
            time = time / 60;
        }
        if (type > 1) {
            time = time / 60;
        }
        if (type > 2) {
            time = time / 24;
        }
        return time;
    }

    /**
     * 以指定日期格式返回日期字符串
     *
     * @param date    yyyy-MM-dd格式的日期字符串
     * @param pattern 日期时间格式(如:yyyy-MM-dd,MM-dd-yyyy等)
     * @return 指定日期格式返回日期字符串
     */
    public static String getDateStr(String date, String pattern) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date theDate = format.parse(date);
            format.applyPattern(pattern);
            return format.format(theDate);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取日期字符串
     *
     * @return
     */
    public static String getDateStr() {
        Calendar cld = Calendar.getInstance();
        long bjTimeRowOff = (long) TimeZone.getTimeZone("GMT+8").getRawOffset();
        long defaultTimeRowOff = (long) TimeZone.getDefault().getRawOffset();
        if (bjTimeRowOff != defaultTimeRowOff) {
            cld.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        }

        String year = Integer.toString(cld.get(1));
        String month = Integer.toString(cld.get(2) + 1);
        if (month.length() == 1) {
            month = '0' + month;
        }

        String day = Integer.toString(cld.get(5));
        if (day.length() == 1) {
            day = '0' + day;
        }

        return year + month + day;
    }

    /**
     * 以指定日期格式返回日期字符串
     *
     * @param date    Date类型
     * @param pattern 日期时间格式(如:yyyy-MM-dd,MM-dd-yyyy等)
     * @return 指定格式的日期字符串
     */
    public static String getDate(Date date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    /**
     * 以指定日期格式返回UTC日期字符串
     *
     * @param date    yyyy-MM-dd格式的日期字符串
     * @param pattern 日期时间格式(如:yyyy-MM-dd,MM-dd-yyyy等)
     * @return 指定日期格式的UTC日期字符串
     */
    @SuppressWarnings("deprecation")
    public static String getUtcDate(String date, String pattern) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date now = Calendar.getInstance().getTime();
            Date theDate = format.parse(date);
            theDate.setHours(now.getHours());
            theDate.setMinutes(now.getMinutes());
            format.setTimeZone(TimeZone.getTimeZone("UTC"));
            format.applyPattern(pattern);
            return format.format(theDate);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 以指定日期格式返回UTC日期
     *
     * @param date    yyyy-MM-dd格式的日期字符串
     * @param pattern 日期时间格式(如:yyyy-MM-dd,MM-dd-yyyy等)
     * @return 指定日期格式的UTC日期字符串
     */
    public static Date getDate(String date, String pattern) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            return format.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 当前日期增加days天
     *
     * @param days 天数
     * @return 增加days天后的日期
     */
    public static Date add(int days) {
        Calendar now = Calendar.getInstance();
        now.add(Calendar.DAY_OF_YEAR, days);
        return now.getTime();
    }

    /**
     * 给当前日期增加days天，并以指定日期格式返回的字符串
     *
     * @param days    天数
     * @param pattern 日期时间格式(如:yyyy-MM-dd,MM-dd-yyyy等)
     * @return 指定日期格式的字符串
     */
    public static String add(int days, String pattern) {
        Date date = add(days);
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    /**
     * 给指定日期增加days天
     *
     * @param date yyyyMMdd格式的日期
     * @param days 天数
     * @return yyyyMMdd格式的日期字符串
     */
    public static String add(int date, int days) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
            Calendar cd = Calendar.getInstance();
            cd.setTime(format.parse(String.valueOf(date)));
            cd.add(Calendar.DAY_OF_YEAR, days);
            return format.format(cd.getTime());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 给指定日期增加days天，并以指定日期格式返回的字符串
     *
     * @param date    yyyy-MM-dd格式的日期字符串
     * @param days    天数
     * @param pattern 日期时间格式(如:yyyy-MM-dd,MM-dd-yyyy等)
     * @return 指定日期格式的字符串
     */
    public static String add(String date, int days, String pattern) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cd = Calendar.getInstance();
            cd.setTime(format.parse(date));
            cd.add(Calendar.DAY_OF_YEAR, days);
            return new SimpleDateFormat(pattern).format(cd.getTime());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 给指定日期增加hours小时，并以指定日期格式返回的字符串
     *
     * @param date  yyyyMMdd格式的日期
     * @param hours 小时数
     * @return
     */
    public static String addHours(int date, int hours) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
            Calendar cd = Calendar.getInstance();
            cd.setTime(format.parse(String.valueOf(date)));
            cd.add(Calendar.HOUR_OF_DAY, hours);
            return format.format(cd.getTime());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 给指定日期增加hours小时，并以指定日期格式返回的字符串
     *
     * @param date    yyyy-MM-dd格式的日期字符串
     * @param hours   小时数
     * @param pattern 日期时间格式(如:yyyy-MM-dd,MM-dd-yyyy等)
     * @return 指定日期格式的字符串
     */
    public static String addHours(String date, int hours, String pattern) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cd = Calendar.getInstance();
            cd.setTime(format.parse(date));
            cd.add(Calendar.HOUR_OF_DAY, hours);
            return new SimpleDateFormat(pattern).format(cd.getTime());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 给指定日期增加hours小时
     *
     * @param date  Date类型的日期
     * @param hours 小时数
     * @return 修改后的日期
     */
    public static Date addHours(Date date, int hours) {
        try {
            Calendar cd = Calendar.getInstance();
            cd.setTime(date);
            cd.add(Calendar.HOUR_OF_DAY, hours);
            return cd.getTime();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 给指定日期增加分钟，并以指定日期格式返回的字符串
     *
     * @param date   Date类型日期
     * @param minute 分钟数
     * @return
     */
    public static Date addMinute(Date date, int minute) {
        try {
            Calendar cd = Calendar.getInstance();
            cd.setTime(date);
            cd.add(Calendar.MINUTE, minute);
            return cd.getTime();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String formatTimeStamp(long bornTimestamp) {
        Timestamp ts = new Timestamp(bornTimestamp);
        return SDF_FULL.format(ts);
    }

    public static Date offsetMinute(Date date, int i) {
        Calendar cd = Calendar.getInstance();
        cd.setTime(date);
        cd.add(Calendar.MINUTE, i);
        return cd.getTime();
    }

    public static String formatDateYMDHM(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_minute_PATTERN);

        if (date == null) {
            return "";
        } else {
            return sdf.format(date);
        }
    }

    public static String formatDate(Date date) {
        if (date == null) {
            return "";
        } else {
            return SDF_FULL.format(date);
        }
    }

    public static String formatDateYMD(Date date) {
        if (date == null) {
            return "";
        } else {
            return SDF_YMD.format(date);
        }
    }

    public static Date parseStringToDate(String str) {
        if (StringUtils.isEmpty(str)) {
            return null;
        } else {
            try {
                return SDF_FULL.parse(str);
            } catch (ParseException e) {
                e.printStackTrace();
                return parseStringToDateYMD(str);
            }
        }
    }

    public static Date parseStringToDateYMD(String str) {
        if (StringUtils.isEmpty(str)) {
            return null;
        } else {
            try {
                return SDF_YMD.parse(str);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 剩余时间秒
     *
     * @param d1 时间1
     * @param d2 时间2
     * @return 6时36分
     */
    public static Long buildSurplusTime(Date d1, Date d2) {
        Long surplusTime = 0L;
        if (d1 != null && d2 != null) {
            long surplus = d1.getTime() - d2.getTime();
            if (surplus > 0) {
                surplusTime = surplus;
            }
        }
        return surplusTime;
    }

    /**
     * 剩余时间xx天xx时xx秒
     *
     * @param d1 时间1
     * @param d2 时间2
     * @return 6时36分
     */
    public static String buildTime(Date d1, Date d2) {
        String time = "";
        if (d1 != null && d2 != null) {
            long surplusTime = d1.getTime() - d2.getTime();
            if (surplusTime > 0) {
                time = buildTime(surplusTime);
            }
        }
        return time;
    }

    public static String timeStamp2Date(String seconds, String format) {
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            return "";
        }
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf2 = new SimpleDateFormat(format);
        return sdf2.format(new Date(Long.parseLong(seconds+"000")));
    }

    public static String millisecondTimeStamp2Date(String seconds, String format) {
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            return "";
        }
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf2 = new SimpleDateFormat(format);
        return sdf2.format(new Date(Long.parseLong(seconds)));
    }

    /**
     * xx天xx时xx秒
     *
     * @param surplusTime 时间戳
     * @return 6时36分
     */
    public static String buildTime(long surplusTime) {
        String str = "";
        long second = surplusTime / 1000;// 秒
        long minute = surplusTime / 1000 / 60;// 分
        long hour = surplusTime / 1000 / 60 / 60;// 时
        long day = surplusTime / 1000 / 60 / 60 / 24;// 时
        if (day != 0) {
            str = str + day + "天";
        }
        if (hour != 0) {
            str = str + hour % 24 + "时";
        }
        if (minute == 0) {
            str = str + second + "秒";
        } else {
            str = str + minute % 60 + "分";
        }
        return str;
    }

    public static Date addDay(Date now, Integer day) {
        Calendar c = Calendar.getInstance();
        c.setTime(now);
        // c.add(Calendar.DAY_OF_MONTH, day);
        c.set(Calendar.DATE, c.get(Calendar.DATE) + day);
        return c.getTime();
    }

    /**
     * 指定日期减指定天数
     *
     * @param now
     * @param day
     * @return
     */
    public static Date subtractDay(Date now, Integer day) {
        Calendar c = Calendar.getInstance();
        c.setTime(now);
        // c.add(Calendar.DAY_OF_MONTH, day);
        c.set(Calendar.DATE, c.get(Calendar.DATE) - day);
        return c.getTime();
    }

    /**
     * 日期减指定天数后日期的开始时间
     *
     * @param date
     * @param day
     * @return 2019-08-21 00:00:00
     */
    public static Date getSubtractDayBeginTime(Date date, Integer day) {
        return MyDateUtils.subtractDay(MyDateUtils.getBeginTime(date), day);// 注意今天开始时间少减一天
    }

    /**
     * 日期减指定天数后日期的开始时间
     *
     * @param date
     * @param day
     * @return 2019-08-21 00:00:00
     */
    public static Date getSubtractDayEndTime(Date date, Integer day) {
        return MyDateUtils.subtractDay(MyDateUtils.getEndTime(date), day);// 注意今天开始时间少减一天
    }

    /**
     * @param date
     * @param day
     * @return 2019-08-21 00:00:00
     */
    public static Date getAddDayBeginTime(Date date, Integer day) {
        return MyDateUtils.addDay(MyDateUtils.getBeginTime(date), day);
    }

    /**
     * @param date
     * @param day
     * @return 2019-08-21 00:00:00
     */
    public static Date getAddDayEndTime(Date date, Integer day) {
        return MyDateUtils.addDay(MyDateUtils.getEndTime(date), day);//
    }

    /**
     * 获取指定日期距离现在的天数
     *
     * @param beforeDate
     * @return
     */
    public static long getDayCount(Date beforeDate) {
        if (beforeDate == null) {
            return 0;
        }
        long beforeTime = beforeDate.getTime();
        long nowTime = new Date().getTime();
        long time = nowTime - beforeTime;// 相差时间
        long day = time / (24 * 60 * 60 * 1000);
        return day;
    }

    /**
     * 减去
     *
     * @param appoTimeEnd
     * @param hour
     * @return
     */
    public static Date getSubtractHour(Date appoTimeEnd, int hour) {
        if (appoTimeEnd == null) {
            return null;
        }
        long begin = appoTimeEnd.getTime() - hour * 60 * 60 * 1000;
        return new Date(begin);
    }

    public static Date reduceHour(Date date, int hour) {
        if (date == null) {
            return null;
        }
        long time = date.getTime() - (hour * 60 * 60 * 1000);
        return new Date(time);
    }

    public static Date increaseHour(Date date, int hour) {
        if (date == null) {
            return null;
        }
        long time = date.getTime() + (hour * 60 * 60 * 1000);
        return new Date(time);
    }

    /**
     * 获取一天的开始的第一秒
     *
     * @param dateString 需要转换的时间，格式"yyyy-MM-dd"
     * @return
     */
    public static String getStartTime(String dateString) {
        dateString = StringUtils.trim(dateString);
        Date date = parseStringToDateYMD(dateString);
        if (date == null) {
            return null;
        }

        String[] s = dateString.split(" ");
        return s[0] + " 00:00:00";
    }

    /**
     * 获取一天的结束的第最后一秒
     *
     * @param dateString 需要转换的时间，格式"yyyy-MM-dd"
     * @return
     */
    public static String getEndTime(String dateString) {
        dateString = StringUtils.trim(dateString);
        Date date = parseStringToDateYMD(dateString);
        if (date == null) {
            return null;
        }

        String[] s = dateString.split(" ");
        return s[0] + " 23:59:59";
    }

    /**
     * 获取本月第一天
     *
     * @param date 当前日期
     * @return
     */
    public static Date getMonthFirstDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, 0);
        return calendar.getTime();
    }

    /**
     * 获取本月最后一天
     *
     * @param date 当前日期
     * @return
     */
    public static Date getMonthLastDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        calendar.add(Calendar.MONTH, 1);
        return calendar.getTime();
    }

    /**
     * 获取上个月第一天
     *
     * @return
     */
    public static Date getLastMonthFirstDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, -1);
        return calendar.getTime();
    }

    /**
     * 计算两个日期(仅日期)相差的天数
     *
     * @param beginDate 日期(小)
     * @param endDate   日期(大)
     * @return
     */
    public static int diffDateDays(Date beginDate, Date endDate) {
        if (beginDate == null || endDate == null) {
            return 0;
        }

        Calendar c = Calendar.getInstance();
        c.setTime(beginDate);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);

        Calendar c2 = Calendar.getInstance();
        c2.setTime(endDate);
        c2.set(Calendar.HOUR_OF_DAY, 0);
        c2.set(Calendar.MINUTE, 0);
        c2.set(Calendar.SECOND, 0);

        return (int) ((c2.getTime().getTime() - c.getTime().getTime()) / (1000 * 3600 * 24));
    }
}